package de.backtomedieval.activities;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import de.backtomedieval.R;
import de.backtomedieval.game.Player;
import de.backtomedieval.main.Game;

public class TopBar {

	private RelativeLayout topbar;
	private Activity myActivity;
	
	public TopBar(Activity caller) {
		
		this.myActivity = caller;
		
		LinearLayout wrapper = (LinearLayout) caller.findViewById(R.id.topbar_wrapper);
		topbar = (RelativeLayout) View.inflate(caller, R.layout.topbar, null);
		wrapper.addView(topbar);
		
		TextView tv_rounds  = (TextView) topbar.findViewById(R.id.rounds);
		tv_rounds .setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Game.setGamePaused(!Game.isGamePaused());
				TopBar.this.refreshCityInfo();
			}
		});

		TextView tv_title = (TextView) topbar.findViewById(R.id.title);
		tv_title.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Game.getGame().showChangeCityDialog(myActivity);
			}
		});
	}
	
	
	public void setTitle(String title) {
		TextView tv_title = (TextView) topbar.findViewById(R.id.title);
		tv_title.setText(title); 
	}
	
	
	public void refreshCityInfo() {
		
       TextView tvfood = (TextView) topbar.findViewById(R.id.waresfood);
       TextView tvgold = (TextView) topbar.findViewById(R.id.waresgold);
       TextView tvlumber = (TextView) topbar.findViewById(R.id.wareslumber);
       TextView tvstone = (TextView) topbar.findViewById(R.id.waresstone);
       TextView tviron = (TextView) topbar.findViewById(R.id.waresiron);
       TextView rounds = (TextView) topbar.findViewById(R.id.rounds);
		
		Player p = Game.getGame().getPlayer();
		String pause ="[" + Game.getString(R.string.clickforpause) + "]";
		if (Game.isGamePaused()) {
			pause = "[" + Game.getString(R.string.clickforcontinue) + "]";
		}
		
		rounds.setText(Game.getGame().getRoundnr() + "  " + pause);
		tvfood.setText(""+p.getCurrentCity().getWares(0));
		tvgold.setText(""+p.getCurrentCity().getWares(1));
		tvlumber.setText(""+p.getCurrentCity().getWares(2));
		tvstone.setText(""+p.getCurrentCity().getWares(3));
		tviron.setText(""+p.getCurrentCity().getWares(4));
		int[] updateWares = p.getCurrentCity().getUpdateWares();
		if (updateWares[0]<0) {
			tvfood.setTextColor(Color.RED);
		} else {
			tvfood.setTextColor(Color.WHITE);
		}
		if (updateWares[1]<0) {
			tvgold.setTextColor(Color.RED);
		} else {
			tvgold.setTextColor(Color.WHITE);
		}
		if (updateWares[2]<0) {
			tvlumber.setTextColor(Color.RED);
		} else {
			tvlumber.setTextColor(Color.WHITE);
		}
		if (updateWares[3]<0) {
			tvstone.setTextColor(Color.RED);
		} else {
			tvstone.setTextColor(Color.WHITE);
		}
		if (updateWares[4]<0) {
			tviron.setTextColor(Color.RED);
		} else {
			tviron.setTextColor(Color.WHITE);
		}
	}
	
	
}
